<script>
window.apiready = function() {
	api.igetPrefs = function(key) {
		var keys = key.split('.');
		var data = '';
		if(keys.length == 1) {
			data = api.getPrefs({
				sync: true,
				key: key
			});
			try {
				data = JSON.parse(data);
			} catch(e) {}
		} else if(keys.length == 2) {
			var all = api.igetPrefs(keys[0]);
			if(all && all[keys[1]]) {
				data = all[keys[1]];
			}
		} else if(keys.length == 3) {
			var all = api.igetPrefs(keys[0]);
			if(all && all[keys[1]]) {
				data = all[keys[1]][keys[2]];
			}
		}
		return data;
	};

	api.isetPrefs = function(key, params) {
		var keys = key.split('.');
		if(keys.length == 1) {
			api.setPrefs({
				key: key,
				value: params
			});
		} else if(keys.length == 2) {
			var all = api.igetPrefs(keys[0]);
			if(!all) {
				all = {};
			}
			all[keys[1]] = params;
			api.setPrefs({
				key: keys[0],
				value: all
			});
		} else if(keys.length == 3) {
			var all = api.igetPrefs(keys[0]);
			if(!all) {
				all = {};
			}
			if(!all[keys[1]]) {
				all[keys[1]] = {};
			}
			all[keys[1]][keys[2]] = params;
			api.setPrefs({
				key: keys[0],
				value: all
			});
		}
		return true;
	}

	require(['cloud'], function(cloud){
		cloud.init({
			relation: {php echo json_encode($relation);}
		});
	});
	require(['apicloud'], function(){});
};

$(function(){
	wx.ready(function () {
		sharedata = {
			title: "{$_share['title']}",
			desc: "{$_share['desc']}",
			link: "{$_share['link']}",
			imgUrl: "{$_share['imgUrl']}",
			success: function(){},
			cancel: function(){}
		};
		wx.onMenuShareAppMessage(sharedata);
		wx.onMenuShareTimeline(sharedata);
	});

	if($("#scanqrcode").size() > 0) {
		$(document).on('click', '#scanqrcode', function(){
			wx.ready(function(){
				wx.scanQRCode({
					needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
					scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
					success: function (res) {
						var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
					}
				});
			});
		});
	}

	if($(".js-date").size() > 0) {
		$('.js-date .calendar').each(function(){
			var $js_date = $(this).parent();
			$(this).calendar({
				closeByOutsideClick: true,
				cssClass: 'modal-in remove-on-close',
				maxDate: $js_date.data('maxDate'),
				onChange: function(a, b , c) {
					$.showIndicator();
					$js_date.find('.calendar').addClass('hide');
					if($js_date.data('href')) {
						location.href = $js_date.data('href') + '&date=' + c[0];
					}
					event.stopPropagation();
					return false;
				}
			});
		});
		$('.js-date .date').click(function(){
			$('.js-date .calendar').toggleClass('hide');
		});
		$('.content').click(function(){
			$('.js-date .calendar').addClass('hide');
		});
	}

	$(document).off('click', '.photoBrowser-image-item');
	$(document).on('click', '.photoBrowser-image-item', function(){
		var $parents = $(this).parent();
		var thumbs = [];
		$($parents).find('img').each(function(){
			var thumb = $(this).attr('src');
			if(thumb) {
				thumbs.push(thumb);
			}
		});
		var myPhotoBrowserPopup = $.photoBrowser({
			photos: thumbs
		});
		myPhotoBrowserPopup.open();
	});

	if($("img.lazyload").size() > 0) {
		require(['jquery.lazyload'], function(){
			$("img.lazyload").lazyload({
				container: $('.lazyload-container'),
				effect : 'fadeIn',
				threshold : 350
			});
		});
	}

	$(document).on("click", '.js-open-modal', function(e) {
		e.preventDefault();
		var modal = $(this).data('modal');
		if(!modal || !$(modal).size()) {
			return false;
		}
		$.iopenModal(modal, function(){});
	});

	if($('.btn-captcha').length > 0) {
		$(document).on('click', '.btn-captcha', function() {
			var href = $(this).data('href');
			href = href + Math.random();
			$(this).attr('src', href);
		});
	}

	if($(".checkbox").length > 0) {
		$(document).on('click', '.js-checkbox', function(){
			var obj = $(this), confirm = obj.data("confirm"), url = obj.data("href"), value = obj.prop('checked') ? (obj.attr('value') || 1) : 0, name = obj.attr('name');
			handler = function() {
				if(obj.attr("submitting") == "1") {
					return;
				}
				obj.attr("submitting", 1);
				$.showIndicator();
				var params = {};
				params[name] = value;
				$.post(url, params, function(ret) {
					$.hideIndicator();
					var result = $.parseJSON(ret);
					var errno = result.message.errno,
							url = result.message.url ? result.message.url : '',
							message = result.message.message;
					if(!errno) {
						$.toast(message, url);
					} else {
						$.toast(message, url);
						obj.removeAttr("submitting").html(html);
					}
				}).fail(function() {
					obj.removeAttr("submitting");
					$.toast('网络异常');
				});
			};
			if(confirm) {
				$.confirm(confirm, handler);
			} else {
				handler();
			}
		});
	}

	$(document).on("click", '.js-post', function(e) {
		e.preventDefault();
		var obj = $(this), confirm = obj.data("confirm"), url = obj.data("href") || obj.attr("href"), data = obj.data("set") || {}, html = obj.html();
		handler = function() {
			e.preventDefault();
			if(obj.attr("submitting") == "1") {
				return;
			}
			obj.attr("submitting", 1);
			$.showIndicator();
			$.post(url, {data: data}, function(ret) {
				$.hideIndicator();
				var result = $.parseJSON(ret);
				var errno = result.message.errno,
						url = result.message.url ? result.message.url : '',
						message = result.message.message;
				if(!errno) {
					$.toast(message, url);
				} else {
					$.toast(message, url);
					obj.removeAttr("submitting").html(html);
				}
			}).fail(function() {
				obj.removeAttr("submitting");
				$.toast('网络异常');
			});
		};
		if(confirm) {
			$.confirm(confirm, handler);
		} else {
			handler();
		}
		return false;
	});

	$(document).on("click", '.js-modal', function(e) {
		e.preventDefault();
		var obj = $(this), confirm = obj.data("confirm");
		var handler = function() {
			$("#js-modal").remove(), e.preventDefault();
			var url = obj.data("href") || obj.attr("href"), data = obj.data("set"), modal;
			$.ajax(url, {
				type: "get",
				dataType: "html",
				cache: false,
				data: data
			}).done(function(result) {
				$.hideIndicator();
				if (result.substr(0, 10) == '{"message"') {
					var json = eval("(" + result + ")");
					var errno = json.message.errno, message = json.message.message;
					if(errno) {
						$.toast(message);
						return;
					}
				}
				modal = $('<div class="popup" id="js-modal"></div>');
				$(document.body).append(modal);
				modal.iappend(result, function() {
					$.popup('#js-modal');
				});
			});
		};
		if (confirm) {
			$.confirm(confirm, handler);
		} else {
			handler();
		}
	});

	$(document).on("infinite", '.js-infinite', function() {
		var $this = $(this);
		var id = $this.data('min'), href = $this.data('href'), tpl = $this.data('tpl'), container = $this.data('container');
		if(!id || !href || !tpl || $this.data('loading') == 1) return false;
		$this.data('loading', 1);
		$this.find('.infinite-scroll-preloader').removeClass('hide');
		$.post(href, {min: id, time: timeStamp}, function(data){
			var result = $.parseJSON(data);
			if(result.message.errno != 0) {
				$.toast(result.message.message);
				return;
			}
			$this.attr('data-min', result.message.min) || $this.data('min', result.message.min);
			if(!result.message.min) {
				$.detachInfiniteScroll($('.infinite-scroll'));
				$('.infinite-scroll-preloader').remove();
				return;
			}
			$this.find('.infinite-scroll-preloader').removeClass('hide');
			$this.data('loading', 0);
			var tpl = $('#' + $this.data('tpl')).html();
			require(['laytpl'], function(laytpl){
				laytpl(tpl).render(result.message.message, function(html){
					$this.find(container).append(html);
				});
			});
		});
	});

	$(document).on('click', '.js-url', function(){
		var url = $(this).data('link');
		if(url) {
			location.href = url;
		}
	});

	setInterval(function(){
		$.post("{php echo imurl('system/common/cron')}", function(){});
	}, 30000);
	$.init();
});
</script>
</body>
</html>